package com.liu.network.rpc;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.apache.log4j.Logger;

import java.util.ResourceBundle;

/**
 * @description: TODO
 * @author: zengjiahuan
 * @data: 2019/6/2 20:18
 * @version: V1.0
 **/
public class CommonRpc {
    private static final Logger logger = Logger.getLogger(CommonRpc.class);
    private static ResourceBundle resb = null;
    private static String domain;
    private static String version;
    private static String regionId;
    private static String signName;
    private static String templateCode;
    private static String accessKeyId;
    private static String accessSecret;

    static {
        resb = ResourceBundle.getBundle("sms");
        System.out.println("sms");
        domain = resb.getString("ali.domain");
        version = resb.getString("ali.version");
        regionId = resb.getString("ali.RegionId");
        signName = resb.getString("ali.SignName");
        templateCode = resb.getString("ali.TemplateCode");
        accessKeyId = resb.getString("ali.accessKeyId");
        accessSecret = resb.getString("ali.accessSecret");
        System.out.println(signName);
    }

    public static String sendMessage(String code, String phone) {
        logger.info("send msg");
        String error = "";
        if (code == null || code.trim().length() == 0 || phone == null || phone.trim().matches("\\d{11}") == false) {
            logger.error("参数错误：code:" + code + ";phone:" + phone);
            return "参数错误";
        }
        boolean ret = false;
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain(domain);
        request.setVersion(version);
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", regionId);
        request.putQueryParameter("PhoneNumbers", phone.trim());
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + code.trim() + "\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            JSONObject jo = JSONObject.parseObject(response.getData());
            if (jo.containsKey("Code") && "OK".equals(jo.getString("Code"))) {
                ret = true;
            } else {
                logger.error(response.getData());
                if (jo.containsKey("Message")) {
                    error = jo.getString("Message");
                }
            }
        } catch (ServerException e) {
            e.printStackTrace();
            logger.error(e);
            error = "ServerException";
        } catch (ClientException e) {
            e.printStackTrace();
            logger.error(e);
            System.out.println(e.getMessage());
            error = "ClientException";
        }
        if (ret) {
            return "success";
        } else {
            return error;
        }
    }

}
